// MFCControlCtrl.cpp : Implementation of the CMFCControlCtrl ActiveX Control class.

#include "stdafx.h"
#include "MFCControl.h"
#include "MFCControlCtrl.h"
#include "MFCControlPropPage.h"
#include "afxdialogex.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNCREATE(CMFCControlCtrl, COleControl)

	int WM_NEXT_FILE;

// Message map

BEGIN_MESSAGE_MAP(CMFCControlCtrl, COleControl)
	ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
	ON_WM_CREATE()
END_MESSAGE_MAP()



// Dispatch map

BEGIN_DISPATCH_MAP(CMFCControlCtrl, COleControl)
	DISP_FUNCTION_ID(CMFCControlCtrl, "FilePlay", dispidFilePlay, FilePlay, VT_I4, VTS_PBSTR)
END_DISPATCH_MAP()



// Event map

BEGIN_EVENT_MAP(CMFCControlCtrl, COleControl)
	EVENT_CUSTOM_ID("OnNextTrack", eventidOnNextTrack, OnNextTrack, VTS_NONE)
	EVENT_CUSTOM_ID("OnDelPlaylist", eventidOnDelPlaylist, OnDelPlaylist, VTS_NONE)
	EVENT_CUSTOM_ID("OnAddFile", eventidOnAddFile, OnAddFile, VTS_PBSTR)
	EVENT_CUSTOM_ID("OnPreviousTrack", eventidOnPreviousTrack, OnPreviousTrack, VTS_NONE)
END_EVENT_MAP()



// Property pages

// TODO: Add more property pages as needed.  Remember to increase the count!
BEGIN_PROPPAGEIDS(CMFCControlCtrl, 1)
	PROPPAGEID(CMFCControlPropPage::guid)
END_PROPPAGEIDS(CMFCControlCtrl)



// Initialize class factory and guid

IMPLEMENT_OLECREATE_EX(CMFCControlCtrl, "MFCCONTROL.MFCControlCtrl.1",
	0xe3dfed94, 0xefe2, 0x42de, 0xa5, 0x8a, 0x2a, 0x4f, 0xd4, 0x2d, 0xe3, 0xad)



// Type library ID and version

IMPLEMENT_OLETYPELIB(CMFCControlCtrl, _tlid, _wVerMajor, _wVerMinor)



// Interface IDs

const IID IID_DMFCControl = { 0x5183C0E2, 0xE2E5, 0x4F0A, { 0xBB, 0xB5, 0x2E, 0x2D, 0x2A, 0x3, 0x5A, 0x4F } };
const IID IID_DMFCControlEvents = { 0x8A35DC24, 0xEA5, 0x4B9E, { 0xA0, 0xD6, 0x2, 0x18, 0x60, 0xF5, 0xF8, 0xD7 } };


// Control type information

static const DWORD _dwMFCControlOleMisc =
	OLEMISC_ACTIVATEWHENVISIBLE |
	OLEMISC_SETCLIENTSITEFIRST |
	OLEMISC_INSIDEOUT |
	OLEMISC_CANTLINKINSIDE |
	OLEMISC_RECOMPOSEONRESIZE;

IMPLEMENT_OLECTLTYPE(CMFCControlCtrl, IDS_MFCCONTROL, _dwMFCControlOleMisc)



// CMFCControlCtrl::CMFCControlCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CMFCControlCtrl

BOOL CMFCControlCtrl::CMFCControlCtrlFactory::UpdateRegistry(BOOL bRegister)
{
	// TODO: Verify that your control follows apartment-model threading rules.
	// Refer to MFC TechNote 64 for more information.
	// If your control does not conform to the apartment-model rules, then
	// you must modify the code below, changing the 6th parameter from
	// afxRegApartmentThreading to 0.

	if (bRegister)
		return AfxOleRegisterControlClass(
			AfxGetInstanceHandle(),
			m_clsid,
			m_lpszProgID,
			IDS_MFCCONTROL,
			IDB_MFCCONTROL,
			afxRegApartmentThreading,
			_dwMFCControlOleMisc,
			_tlid,
			_wVerMajor,
			_wVerMinor);
	else
		return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}



// CMFCControlCtrl::CMFCControlCtrl - Constructor

CMFCControlCtrl::CMFCControlCtrl()
{
	InitializeIIDs(&IID_DMFCControl, &IID_DMFCControlEvents);
	// TODO: Initialize your control's instance data here.
}



// CMFCControlCtrl::~CMFCControlCtrl - Destructor

CMFCControlCtrl::~CMFCControlCtrl()
{
	// TODO: Cleanup your control's instance data here.
}



// CMFCControlCtrl::OnDraw - Drawing function

void CMFCControlCtrl::OnDraw(
			CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
	if (!pdc)
		return;

	// TODO: Replace the following code with your own drawing code.
	//pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
	//pdc->Ellipse(rcBounds);
	if(m_PlayerDialog.GetSafeHwnd())
	{
		m_PlayerDialog.MoveWindow(rcBounds, TRUE);
		CBrush brBackGnd(TranslateColor(AmbientBackColor()));
		pdc->FillRect(rcBounds, &brBackGnd);
	}
}



// CMFCControlCtrl::DoPropExchange - Persistence support

void CMFCControlCtrl::DoPropExchange(CPropExchange* pPX)
{
	ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
	COleControl::DoPropExchange(pPX);

	// TODO: Call PX_ functions for each persistent custom property.
	try
	{
		PX_String(pPX, _T("url"), m_FileName, _T(""));
		m_PlayerDialog.m_IsHTML=true;
		m_PlayerDialog.m_FileName=m_FileName;
	}
	catch(...)
	{
	}
}



// CMFCControlCtrl::GetControlFlags -
// Flags to customize MFC's implementation of ActiveX controls.
//
DWORD CMFCControlCtrl::GetControlFlags()
{
	DWORD dwFlags = COleControl::GetControlFlags();


	// The control will not be redrawn when making the transition
	// between the active and inactivate state.
	dwFlags |= noFlickerActivate;
	return dwFlags;
}



// CMFCControlCtrl::OnResetState - Reset control to default state

void CMFCControlCtrl::OnResetState()
{
	COleControl::OnResetState();  // Resets defaults found in DoPropExchange

	// TODO: Reset any other control state here.
}



// CMFCControlCtrl message handlers


int CMFCControlCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (COleControl::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here
	m_PlayerDialog.Create(IDD_PLAYER_DIALOG, this);
	return 0;
}


HRESULT CMFCControlCtrl::FilePlay(BSTR* FilePath)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	m_PlayerDialog.m_FileName=*FilePath;

	if(m_PlayerDialog.m_PlayStatus==m_PlayerDialog.FILE_STOP)
	{
		m_PlayerDialog.m_PlayStatus=m_PlayerDialog.NO_FILE;
		m_PlayerDialog.m_CurPos=0;
	}

	if(m_PlayerDialog.m_PlayStatus!=m_PlayerDialog.NO_FILE)
		m_PlayerDialog.StopPlaying();
	m_PlayerDialog.StartPlaying();

	return S_OK;
}

